package com.example.sudokugame.adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.sudokugame.databinding.ItemLeaderboardBinding
import com.example.sudokugame.models.LeaderboardEntry

class LeaderboardAdapter : RecyclerView.Adapter<LeaderboardAdapter.ViewHolder>() {
    
    private var leaderboardData = listOf<LeaderboardEntry>()
    
    fun updateData(newData: List<LeaderboardEntry>) {
        leaderboardData = newData
        notifyDataSetChanged()
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemLeaderboardBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return ViewHolder(binding)
    }
    
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(leaderboardData[position], position + 1)
    }
    
    override fun getItemCount(): Int = leaderboardData.size
    
    class ViewHolder(private val binding: ItemLeaderboardBinding) : RecyclerView.ViewHolder(binding.root) {
        
        fun bind(entry: LeaderboardEntry, rank: Int) {
            binding.tvRank.text = rank.toString()
            binding.tvPlayerName.text = entry.playerName ?: "匿名玩家"
            binding.tvScore.text = entry.score.toString()
            binding.tvTime.text = formatTime(entry.timeSpent)
        }
        
        private fun formatTime(seconds: Long): String {
            val minutes = seconds / 60
            val secs = seconds % 60
            return String.format("%02d:%02d", minutes, secs)
        }
    }
} 